package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.entity.VoAptDetail;
import com.example.entity.VoAptList;
import com.example.entity.ResponseData;
import com.example.entity.VoReadyAppiont;
import com.example.mapper.SchedulingMapper;
import com.example.pojo.Appointment;
import com.example.mapper.AppointmentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 挂号预约表(Appointment)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class AppointmentService{
    /**
     * mapper对象 
     */
    @Resource
    private AppointmentMapper appointmentMapper;
    @Resource
    private SchedulingMapper schedulingMapper;
    
    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param appointment 筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, Appointment appointment) {
        QueryWrapper<Appointment> qw = new QueryWrapper<>();
        //TODO: getByPage appointment qw查询条件待构建
        IPage ipage = new Page(currentPage,pageSize);
        appointmentMapper.selectPage(ipage,qw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()){
            ipage = new Page(ipage.getPages(),pageSize);
            appointmentMapper.selectPage(ipage,qw);
        }
        if (ipage.getTotal()<=0){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),ipage);
    }
    
    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        Appointment appointment = appointmentMapper.selectById(id);
        if(appointment == null || appointment.getId() == null){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),appointment);
    }
    
    /**
     * 新增数据
     *
     * @param  appointment 实体
     * @return 新增结果
     */
    public ResponseData add(Appointment appointment) {
        //插入
        int result1 = appointmentMapper.insert(appointment);
        int result2 = schedulingMapper.updateSchedulingRest(appointment.getTime(), appointment.getDrId());
        if (result1 > 0 && result2 > 0){
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), "预约成功！");
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "！预约失败");
    }
    
    /**
     * 更新数据
     *
     * @param appointment 实体
     * @return 更新结果
     */
    public ResponseData update(Appointment appointment) {
        //更新
        if (appointmentMapper.updateById(appointment)>0){
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }
    
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    public ResponseData delete(Long id) {
        if (appointmentMapper.deleteById(id)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }
    
    /**
     * 批量删除
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList){
        if (appointmentMapper.deleteBatchIds(idList)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 获取所有会话记录
     * @param userId
     * @return
     */
    public ResponseData getAllApt(Long userId) {
        List<VoAptList> voAptLists = appointmentMapper.selectAptList(userId);
        if(voAptLists == null || voAptLists.isEmpty()){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), voAptLists);
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    public ResponseData cancel(Long id) {
        UpdateWrapper<Appointment> uw = new UpdateWrapper<>();
        uw.eq("id", id);
        uw.set("status", -1);
        // 开始取消
        if (appointmentMapper.update(null, uw)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "取消失败，请稍后再试");
    }


    /**
     * 查询会话关联的详细信息
     * @param id
     * @return
     */
    public ResponseData selectAptDetail(Long id) {
        VoAptDetail voAptDetail = appointmentMapper.selectAptDetail(id);
        if(voAptDetail == null || voAptDetail.getAptId() == null){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), voAptDetail);
    }

    /**
     * 根据医生id查询预约列表
     * @param drId
     * @return
     */
    public ResponseData findReady(Long drId) {
        List<VoReadyAppiont> voReadyAppionts = appointmentMapper.selectReadyApmt(drId);
        if(voReadyAppionts == null || voReadyAppionts.isEmpty()){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), voReadyAppionts);
    }
}
